<?php
declare(strict_types=1);

namespace App\Repository\Admin;

use App\Model\Article;

class ArticleRepository
{
    public function getReviewArticles(int $page, int $perPage)
    {
        return Article::with(['user:id,name,avatar'])
            ->latest()
            ->paginate($perPage, page: $page);
    }

    public function getArticleDetail(int $id)
    {
        return Article::with([
            'user:id,name,avatar',
            'comments' => fn($query) => $query->latest()->limit(10)
        ])->find($id);
    }

    public function getReviewArticleCountBy(array $ids): int
    {
        return Article::whereIn('id', $ids)
            ->where('status', 'review')
            ->count();
    }

    public function approve(array $ids, bool $approved)
    {
        return Article::whereIn('id', $ids)
            ->update([
                'status' => $approved ? 'published' : 'review'
            ]);
    }
}